package com.yuxl.common.auth.repository;

import com.yuxl.common.auth.entity.AuthRole;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author yuxl
 */
@PostgreSqlRepository
public interface AuthRoleRepository extends ReactiveCrudRepository<AuthRole, String> {
    @Query("""
            SELECT count(*) FROM auth_role
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            """)
    Mono<Integer> count(AuthRole entity);

    @Query("""
            SELECT * FROM auth_role
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            LIMIT :limit OFFSET :offset
            """)
    Flux<AuthRole> page(AuthRole entity, long limit, long offset);

    @Query("""
            SELECT * FROM auth_role
            WHERE 1=1
            AND (:#{#entity.name} IS NULL OR name LIKE CONCAT('%', :#{#entity.name}, '%'))
            ORDER BY create_time DESC
            """)
    Flux<AuthRole> list(AuthRole entity);


    @Query("""
            UPDATE auth_role
            SET name = :#{#entity.name},
                update_time = :#{#entity.updateTime}
            WHERE mark = :#{#entity.mark}
            RETURNING *
            """)
    Mono<AuthRole> updateById(AuthRole entity);
} 